package com.tianya.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianya.domain.dto.NoticeRequestDTO;
import com.tianya.domain.entity.Notice;
import com.tianya.domain.entity.User;
import com.tianya.domain.vo.NoticeVO;
import com.tianya.domain.vo.PageResultVO;
import com.tianya.mapper.UserMapper;
import com.tianya.service.NoticeService;
import com.tianya.mapper.NoticeMapper;
import com.tianya.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
* @author 86191
* @description 针对表【notice(通知公告表)】的数据库操作Service实现
* @createDate 2025-05-24 18:59:48
*/
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice>
    implements NoticeService{
    @Resource
    private UserMapper userMapper;
    @Override
    public void createNotice(NoticeRequestDTO noticeRequestDTO) {
        Notice notice = Notice.builder()
                .title(noticeRequestDTO.getTitle())
                .content(noticeRequestDTO.getContent())
                .publisherId(noticeRequestDTO.getPublisherId())
                .createdAt(LocalDateTime.now())
                .build();
        save(notice);
    }

    @Override
    public PageResultVO<NoticeVO> pageNotice(Integer pageNum, Integer pageSize) {
        Page<Notice> page = Page.of(pageNum, pageSize);
        Page<Notice> pageResult = page(page, null);
        PageResultVO<NoticeVO> pageVO = new PageResultVO<>();
        pageVO.setTotal(pageResult.getTotal());
        List<NoticeVO> noticeVOList = new ArrayList<>();
        for (Notice notice : pageResult.getRecords()) {
            Long userId = notice.getPublisherId();
            User user = userMapper.selectById(userId);

            String publisher = user==null ? "unknown" : user.getUsername();
            NoticeVO noticeVO = new NoticeVO();
            noticeVO.setPublisher(publisher);
            BeanUtil.copyProperties(notice, noticeVO);
            noticeVOList.add(noticeVO);
        }
        pageVO.setRecords(noticeVOList);
        return pageVO;
    }

    @Override
    public NoticeVO getNotice(Long id) {
        Notice notice = getById(id);
        NoticeVO noticeVO = new NoticeVO();
        BeanUtil.copyProperties(notice, noticeVO);
        return noticeVO;
    }

    @Override
    public void updateNotice(NoticeRequestDTO noticeRequestDTO) {
        Notice notice = Notice.builder()
                .noticeId(noticeRequestDTO.getId())
                .title(noticeRequestDTO.getTitle())
                .content(noticeRequestDTO.getContent())
                .publisherId(noticeRequestDTO.getPublisherId())
                .build();
        updateById(notice);
    }

    @Override
    public List<NoticeVO> listNotice() {
        List<Notice> noticeList = list();
        List<NoticeVO> noticeVOList = new ArrayList<>();
        for (Notice notice : noticeList) {
            Long userId = notice.getPublisherId();
            User user = userMapper.selectById(userId);

            String publisher = user==null ? "unknown" : user.getUsername();
            NoticeVO noticeVO = new NoticeVO();
            noticeVO.setPublisher(publisher);
            BeanUtil.copyProperties(notice, noticeVO);
            noticeVOList.add(noticeVO);
        }
        return noticeVOList;
    }
}




